//
//  ZJHouseInformationCell.m
//  这家测试项目
//
//  Created by IOS_DEVELOPER on 2017/6/8.
//  Copyright © 2017年 陈志凌. All rights reserved.
//

#import "ZJHouseInformationCell.h"

static float width;
static float height;

@implementation ZJHouseInformationCell

- (void)awakeFromNib {
    [super awakeFromNib];
    
    width = (SCREEN_WIDTH - 16) / 6 ;
    height = self.contentView.height - 25;
    
    
    self.houseNum.sd_layout.leftSpaceToView(self.contentView, 8).topSpaceToView(self.contentView, 8).widthIs(width).heightIs(width);
    
    self.houseNum.font = [UIFont systemFontOfSize:[Utils iphone5AutoScaleW:13.0f]];
}

-(void)setModel:(ZJHousePreselectionModel *)model{
    _model = model;
    
    NSArray * arr =  _model.units;
    _model.units = [ZJFloorDetailModel mj_objectArrayWithKeyValuesArray:arr];
    
    self.houseNum.text = [NSString stringWithFormat:@"%@%@",_model.building_name,_model.building_unit];
    
    NSArray *subViews = [self.bgView subviews];

    if([subViews count] != 0) {

        [subViews makeObjectsPerformSelector:@selector(removeFromSuperview)];

    }
    
    NSMutableArray * btnArr = [NSMutableArray new];
    
    for (int i = 0; i < _model.units.count; i++) {
            UIButton * btn = [[UIButton alloc]initWithFrame:CGRectMake(i % 5 * width, i / 5 * height, width, width)];

            ZJFloorDetailModel * Fmodel = _model.units[i];
            [btn setTitle:[NSString stringWithFormat:@"%@%@",Fmodel.unit_name,Fmodel.unit_unit] forState:0];
            [btn setBackgroundColor:[UIColor whiteColor]];
            btn.titleLabel.textAlignment = NSTextAlignmentCenter;
            [btn setTitleColor:[UIColor blackColor] forState:0];
            btn.titleLabel.font = [UIFont systemFontOfSize:[Utils iphone5AutoScaleW:13.0f]];
            btn.tag = i;
            [btn addTarget:self action:@selector(clickBtn:) forControlEvents:(UIControlEventTouchUpInside)];

            btn.layer.borderColor = RGB(235, 235, 235).CGColor;
            btn.layer.borderWidth = 0.5f;
            [self.bgView addSubview:btn];
        
        [btnArr addObject:btn];
        }
    
    if (_model.units.count <1) {
        self.bgView.sd_layout.leftSpaceToView(self.houseNum, 0).topSpaceToView(self.contentView, 8).rightSpaceToView(self.contentView, 8).heightIs(75);
        
    }else{
        self.bgView.sd_layout.leftSpaceToView(self.houseNum, 0).topSpaceToView(self.contentView, 8).rightSpaceToView(self.contentView, 8).autoHeightRatio(0);
        [self.bgView setupAutoHeightWithBottomViewsArray:btnArr bottomMargin:0];
    }
    [self setupAutoHeightWithBottomView:self.bgView bottomMargin:8];
    


//    [self.bgView updateLayout];
    
}

-(void)clickBtn:(UIButton *)sender{
    
    if (_delegate != nil && [_delegate respondsToSelector:@selector(onClikFmodel:)]) {
        ZJFloorDetailModel * Fmodel = _model.units[sender.tag];
        
        [_delegate onClikFmodel:Fmodel];
    }
}

-(void) onClikFmodel:(ZJFloorDetailModel *) Fmodel{
    
    if (_delegate != nil && [_delegate respondsToSelector:@selector(onClikFmodel:)]) {
        [_delegate onClikFmodel:Fmodel];
    }
}
-(void)layoutSubviews{
    [super layoutSubviews];
}
@end
